package com.atguigu.java8.datetime;

import org.junit.Test;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;

public class TestTemporalAdjuster {

    @Test
    public void test1() {
        // LocalDateTime调整日期
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        LocalDateTime localDateTime = now.withDayOfMonth(18);
        System.out.println(localDateTime);

        // 使用TemporalAdjuster调整日期
        LocalDateTime localDateTime1 = now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
        System.out.println(localDateTime1);

        // TemporalAdjuster自定义：下一个工作日
        LocalDateTime nextWorkDay = now.with(temporal -> {
            LocalDateTime dateTime = (LocalDateTime) temporal;
            if (dateTime.getDayOfWeek() == DayOfWeek.FRIDAY) {
                return dateTime.plusDays(3);
            } else if (dateTime.getDayOfWeek() == DayOfWeek.SATURDAY) {
                return dateTime.plusDays(2);
            } else {
                return dateTime.plusDays(1);
            }
        });
        System.out.println(nextWorkDay);
    }

}
